# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

pkgname=perl
pkgver=5.22.0
pkgrel=2
pkgdesc="A highly capable, feature-rich programming language"
arch=(i686 x86_64)
license=('GPL' 'PerlArtistic')
url="http://www.perl.org"
groups=('base-devel')
depends=('db' 'gdbm' 'libcrypt' 'coreutils' 'msys2-runtime' 'sh')
makedepends=('libdb-devel' 'libgdbm-devel' 'libcrypt-devel')
source=(http://www.cpan.org/src/5.0/perl-${pkgver}.tar.bz2
        perlbin.sh
        perlbin.csh
        perl-5.18.0-cygwin.patch
        perl-5.22.0-msys2.patch
        perl-nostdio.patch)
options=('makeflags' '!purge' 'emptydirs')
# NOTE: This array is automatically generated by `./patchprov`.
#       If you want to add entries, do so in the next array.
provides=('perl-Archive-Tar=2.04'
          'perl-Attribute-Handlers=0.97'
          'perl-autodie=2.26'
          'perl-AutoLoader=5.74'
          'perl-autouse=1.08'
          'perl-B-Debug=1.23'
          'perl-base=2.22'
          'perl-bignum=0.39'
          'perl-Carp=1.36'
          'perl-Compress-Raw-Bzip2=2.068'
          'perl-Compress-Raw-Zlib=2.068'
          'perl-Config-Perl-V=0.24'
          'perl-constant=1.33'
          'perl-CPAN-Meta-Requirements=2.132'
          'perl-CPAN-Meta-YAML=0.012'
          'perl-CPAN-Meta=2.150001'
          'perl-CPAN=2.11'
          'perl-Data-Dumper=2.158'
          'perl-DB_File=1.835'
          'perl-Devel-PPPort=3.31'
          'perl-Devel-SelfStubber=1.05'
          'perl-Digest-MD5=2.54'
          'perl-Digest-SHA=5.95'
          'perl-Digest=1.17'
          'perl-Dumpvalue=1.17'
          'perl-Encode=2.72'
          'perl-encoding-warnings=0.11'
          'perl-Env=1.04'
          'perl-experimental=0.013'
          'perl-Exporter=5.72'
          'perl-ExtUtils-CBuilder=0.280221'
          'perl-ExtUtils-Command=1.20'
          'perl-ExtUtils-Constant=0.23'
          'perl-ExtUtils-Install=2.04'
          'perl-ExtUtils-MakeMaker=7.04_01'
          'perl-ExtUtils-Manifest=1.70'
          'perl-ExtUtils-ParseXS=3.28'
          'perl-File-Fetch=0.48'
          'perl-File-Path=2.09'
          'perl-File-Temp=0.2304'
          'perl-Filter-Simple=0.92'
          'perl-Filter-Util-Call=1.54'
          'perl-Getopt-Long=2.45'
          'perl-HTTP-Tiny=0.054'
          'perl-I18N-Collate=1.02'
          'perl-I18N-LangTags=0.40'
          'perl-if=0.0604'
          'perl-IO-Compress=2.068'
          'perl-IO-Socket-IP=0.37'
          'perl-IO-Zlib=1.10'
          'perl-IO=1.35'
          'perl-IPC-Cmd=0.92'
          'perl-IPC-SysV=2.04'
          'perl-JSON-PP=2.27300'
          'perl-lib=0.63'
          'perl-libnet=3.05'
          'perl-Locale-Codes=3.34'
          'perl-Locale-Maketext-Simple=0.21'
          'perl-Locale-Maketext=1.26'
          'perl-Math-BigInt-FastCalc=0.31'
          'perl-Math-BigInt=1.9997'
          'perl-Math-BigRat=0.2608'
          'perl-Math-Complex=1.59'
          'perl-Memoize=1.03'
          'perl-MIME-Base64=3.15'
          'perl-Module-CoreList=5.20150520'
          'perl-Module-Load-Conditional=0.64'
          'perl-Module-Load=0.32'
          'perl-Module-Loaded=0.08'
          'perl-Module-Metadata=1.000026'
          'perl-Net-Ping=2.43'
          'perl-Params-Check=0.38'
          'perl-parent=0.232'
          'perl-Parse-CPAN-Meta=1.4414'
          'perl-PathTools=3.56'
          'perl-Perl-OSType=1.008'
          'perl-perlfaq=5.021009'
          'perl-PerlIO-via-QuotedPrint=0.08'
          'perl-Pod-Checker=1.60'
          'perl-Pod-Escapes=1.07'
          'perl-Pod-Parser=1.63'
          'perl-Pod-Perldoc=3.25'
          'perl-Pod-Simple=3.29'
          'perl-Pod-Usage=1.64'
          'perl-podlators=2.5.3'
          'perl-Safe=2.39'
          'perl-Scalar-List-Utils=1.41'
          'perl-Search-Dict=1.07'
          'perl-SelfLoader=1.22'
          'perl-Socket=2.018'
          'perl-Storable=2.53'
          'perl-Sys-Syslog=0.33'
          'perl-Term-ANSIColor=4.03'
          'perl-Term-Cap=1.15'
          'perl-Term-Complete=1.403'
          'perl-Term-ReadLine=1.15'
          'perl-Test-Harness=3.35'
          'perl-Test-Simple=1.001014'
          'perl-Test=1.26'
          'perl-Text-Abbrev=1.02'
          'perl-Text-Balanced=2.03'
          'perl-Text-ParseWords=3.30'
          'perl-Text-Tabs=2013.0523'
          'perl-Thread-Queue=3.05'
          'perl-Thread-Semaphore=2.12'
          'perl-threads-shared=1.48'
          'perl-threads=2.01'
          'perl-Tie-File=1.01'
          'perl-Tie-RefHash=1.39'
          'perl-Time-HiRes=1.9726'
          'perl-Time-Local=1.2300'
          'perl-Time-Piece=1.29'
          'perl-Unicode-Collate=1.12'
          'perl-Unicode-Normalize=1.18'
          'perl-version=0.9909'
          'perl-XSLoader=0.20')
# Add your own provides here
provides=(${provides[@]})
md5sums=('f67b152160431b3180fb766bdc2d02e2'
         '5ed2542fdb9a60682f215bd33701e61a'
         '1f0cbbee783e8a6d32f01be5118e0d5e'
         '50a495019bbaf298604795e6bb36394a'
         '66e17cd1d891012dd4ab5faaad812acc'
         '26ccfe0cb6348404c4152eb0b3080e65')

prepare() {
  chmod -R 0666 ${srcdir}/${pkgname}-${pkgver}
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p1 -i ${srcdir}/perl-nostdio.patch
  patch -p1 -i ${srcdir}/perl-5.18.0-cygwin.patch
  patch -p1 -i ${srcdir}/perl-5.22.0-msys2.patch

  sed -i 's#version vutil.c .*#version vutil.c f1c7e4778fcf78c04141f562b80183b91cbbf6c9#' t/porting/customized.dat

  cat > Policy.sh <<_EOF
#!/bin/sh
d_readlink='undef'
_EOF

}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  ./Configure -des -Dusethreads \
    -Doptimize="${CFLAGS}" \
    -Dprefix=/usr \
    -Dvendorprefix=/usr \
    -Dprivlib=/usr/share/perl5/core_perl \
    -Darchlib=/usr/lib/perl5/core_perl \
    -Dsitelib=/usr/share/perl5/site_perl \
    -Dsitearch=/usr/lib/perl5/site_perl \
    -Dvendorlib=/usr/share/perl5/vendor_perl \
    -Dvendorarch=/usr/lib/perl5/vendor_perl \
    -Dscriptdir=/usr/bin/core_perl \
    -Dsitescript=/usr/bin/site_perl \
    -Dvendorscript=/usr/bin/vendor_perl \
    -Dinc_version_list=none \
    -Dman1ext=1perl -Dman3ext=3perl \
    -Darchname=${ARCH}-msys-threads \
    -Dmyarchname=${ARCH}-msys \
    -Dlibperl=msys-perl5_22.dll \
    -Dlddlflags="-shared ${LDFLAGS}" -Dldflags="${LDFLAGS}" \
    -Dcc=gcc -Dld=g++

  LC_ALL=C make
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}
  TEST_JOBS=$(echo $MAKEFLAGS | sed 's/.*-j\([0-9][0-9]*\).*/\1/') make test_harness || true
  #make test
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make DESTDIR="$pkgdir" install

  ### Perl Settings ###
  # Change man page extensions for site and vendor module builds.
  # Set no mail address since bug reports should go to the bug tracker
  # and not someone's email.
  sed -e '/^man1ext=/ s/1perl/1p/' -e '/^man3ext=/ s/3perl/3pm/' \
      -e "/^cf_email=/ s/'.*'/''/" \
      -e "/^perladmin=/ s/'.*'/''/" \
      -i ${pkgdir}/usr/lib/perl5/core_perl/Config_heavy.pl

  ### CPAN Settings ###
  # Set CPAN default config to use the site directories.
  sed -e '/(makepl_arg =>/   s/""/"INSTALLDIRS=site"/' \
      -e '/(mbuildpl_arg =>/ s/""/"installdirs=site"/' \
      -i ${pkgdir}/usr/share/perl5/core_perl/CPAN/FirstTime.pm

  # Profile script to set paths to perl scripts.
  install -D -m755 ${srcdir}/perlbin.sh \
                   ${pkgdir}/etc/profile.d/perlbin.sh
  # Profile script to set paths to perl scripts on csh. (FS#22441)
  install -D -m755 ${srcdir}/perlbin.csh \
                  ${pkgdir}/etc/profile.d/perlbin.csh

  # Add the dirs so new installs will already have them in PATH once they
  # install their first perl programm
  install -d -m755 "${pkgdir}/usr/bin/vendor_perl"
  install -d -m755 "${pkgdir}/usr/bin/site_perl"

  (cd ${pkgdir}/usr/bin; mv perl${pkgver} perl)
  (cd ${pkgdir}/usr/bin/core_perl;  ln -sf c2ph pstruct)

  # Remove all pod files *except* those under /usr/lib/perl5/core_perl/pods/
  # (FS#16488)
  rm -f ${pkgdir}/usr/share/perl5/core_perl/*.pod
  for d in ${pkgdir}/usr/share/perl5/core_perl/*; do
    if [ -d $d -a $(basename $d) != "pods" ]; then
      find $d -name *.pod -delete
    fi
  done
  find ${pkgdir}/usr/lib -name *.pod -delete
  find ${pkgdir} -name .packlist -delete

  find ${pkgdir}/usr/lib -type f -exec chmod 0644 {} \;
  find ${pkgdir}/usr/share/perl5 -type f -exec chmod 0644 {} \;
  # prevent xsinit-generated code from referencing boot_Win32CORE
  sed -i -e "s/^\(static_ext\)=.*/\1=' '/" \
    ${pkgdir}/usr/lib/perl5/core_perl/Config_heavy.pl

}
